package cn.wetcland.urban.framework.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.component.RedisCommonKey;
import cn.wetcland.urban.common.component.RedisComponent;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.common.utils.bcrypt.BCryptPasswordEncoder;
import cn.wetcland.urban.framework.dto.CustomerLoginDTO;
import cn.wetcland.urban.framework.dto.DriverLoginDTO;
import cn.wetcland.urban.framework.dto.DriverRegisterDTO;
import cn.wetcland.urban.framework.mapper.UtDistrictMapper;
import cn.wetcland.urban.framework.mapper.UtDriverMapper;
import cn.wetcland.urban.framework.mapper.UtFileUploadMapper;
import cn.wetcland.urban.framework.service.UtDistrictService;
import cn.wetcland.urban.framework.service.UtDriverService;
import cn.wetcland.urban.framework.utils.StpDriverUtil;
import cn.wetcland.urban.framework.vo.CustomerLoginVO;
import cn.wetcland.urban.framework.vo.DriverLoginVO;
import cn.wetcland.urban.generator.domain.UtCustomer;
import cn.wetcland.urban.generator.domain.UtDistrict;
import cn.wetcland.urban.generator.domain.UtDriver;
import cn.wetcland.urban.generator.domain.UtFileUpload;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author Wetcland
* @description 针对表【ut_driver】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtDriverServiceImpl extends ServiceImpl<UtDriverMapper, UtDriver> implements UtDriverService {

    @Resource
    public UtDriverMapper driverMapper;

    @Resource
    public UtFileUploadMapper fileUploadMapper;

    @Resource
    public RedisComponent redisComponent;

    @Resource
    public UtDistrictMapper districtMapper;

    @Resource
    public UtDistrictService districtService;

    @Override
    public DriverLoginVO handleDriverLogin(DriverLoginDTO loginDTO) {
        String s = redisComponent.get(RedisCommonKey.getRedisCommonVerifyCodeKey(loginDTO.getCodeKey()));
        MyAssert.notBlank(s, "codekey不存在");
        MyAssert.isTrue(s.trim().equalsIgnoreCase(loginDTO.getCode()), "验证码错误");
        redisComponent.delete(RedisCommonKey.getRedisCommonVerifyCodeKey(loginDTO.getCodeKey()));
        UtDriver driver = driverMapper.selectOne(new QueryWrapper<UtDriver>().eq("driver_username", loginDTO.getUsername()));
        MyAssert.notNull(driver, "该用户不存在");
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(loginDTO.getPassword(), driver.getDriverPassword());
        MyAssert.isTrue(matches, "密码错误");
        StpDriverUtil.login(driver.getDriverId());
        return getDriverLoginVO(driver);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleDriverRegister(DriverRegisterDTO driverRegisterDTO) {
        String s = redisComponent.get(RedisCommonKey.getRedisCommonVerifyCodeKey(driverRegisterDTO.getCodeKey()));
        MyAssert.notBlank(s, "codekey不存在");
        MyAssert.isTrue(s.trim().equalsIgnoreCase(driverRegisterDTO.getCode()), "验证码错误");
        redisComponent.delete(RedisCommonKey.getRedisCommonVerifyCodeKey(driverRegisterDTO.getCodeKey()));

        UtDriver driver = driverMapper.selectOne(new QueryWrapper<UtDriver>().eq("driver_username", driverRegisterDTO.getDriverUsername()));
        MyAssert.isNull(driver, "该用户名已被注册");
        MyAssert.isTrue(driverRegisterDTO.getDriverPassword().equals(driverRegisterDTO.getDriverConfirmPassword()), "密码与确认密码不一致");
        UtFileUpload utFileUpload = fileUploadMapper.selectById(driverRegisterDTO.getDriverIdcard0());
        MyAssert.notNull(utFileUpload, "身份证人像图片不存在");
        UtFileUpload utFileUpload1 = fileUploadMapper.selectById(driverRegisterDTO.getDriverIdcard1());
        MyAssert.notNull(utFileUpload1, "身份证国徽图片不存在");
        UtFileUpload utFileUpload2 = fileUploadMapper.selectById(driverRegisterDTO.getDriverLicense());
        MyAssert.notNull(utFileUpload2, "驾驶证图片不存在");
        boolean verifiedDistrict = districtService.verifyDistrict(driverRegisterDTO.getProvinceId(), driverRegisterDTO.getCityId(), driverRegisterDTO.getDistrictId());
        MyAssert.isTrue(verifiedDistrict, "省市区不匹配");

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        UtDriver driver1 = new UtDriver();
        CopyObjectUtil.copyPropertiesIgnoreNull(driverRegisterDTO, driver1);
        driver1.setDriverPassword(bCryptPasswordEncoder.encode(driverRegisterDTO.getDriverPassword()));
        driverMapper.insert(driver1);
    }

    private DriverLoginVO getDriverLoginVO(UtDriver driver) {
        DriverLoginVO driverLoginVO = new DriverLoginVO();
        driverLoginVO.setDriverId(driver.getDriverId());
        driverLoginVO.setDriverAge(driver.getDriverAge());
        driverLoginVO.setCreditLevel(driver.getCreditLevel());
        driverLoginVO.setDriverUsername(driver.getDriverUsername());
        driverLoginVO.setDriverGender(driver.getDriverGender());
        driverLoginVO.setProvinceId(driver.getProvinceId());
        driverLoginVO.setCityId(driver.getCityId());
        driverLoginVO.setDistrictId(driver.getDistrictId());
        driverLoginVO.setDriverAvatar(driver.getDriverAvatarImgId());
        driverLoginVO.setToken(StpDriverUtil.getTokenValue());
        driverLoginVO.setDriverPhone(driver.getDriverPhone());
        UtDistrict district = districtMapper.selectById(driver.getCityId());
        driverLoginVO.setAdcode(district.getAdcode());
        return driverLoginVO;
    }
}




